/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  ToolFragment.java   
 * @Package com.jiyi.activity.new_maintenance   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-8-31 下午3:40:20   
 * @version V1.0     
 */
package com.jiyi.activity.new_maintenance;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.jiyi.activity.escape.OwnerListWithMapActivity;
import com.jiyi.activity.leader.UserInfoCheckActivity;
import com.jiyi.activity.owner.OwnerCheckReportActivity;
import com.jiyi.activity.owner.OwnerDataAnalysisActivity;
import com.jiyi.activity.owner.OwnerMaintenanceTaskActivity;
import com.jiyi.activity.owner.repair.OwnerMaintenanceRepairListActivity;
import com.jiyi.bean.maintenance.HireUnitBean;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.inteface.WebApi;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.UserInfoUtil;
import com.swift.utils.DialogTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: ToolFragment
 * @Description:TODO(工具)
 * @author: HYM
 * @date: 2017-8-31 下午3:40:20
 * 
 */
public class ToolOwnerLeaderFragment extends Fragment {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	LayoutInflater inflater;
	ViewGroup container;
	
	Dialog dialog;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.tool_owner_leader_menu, null,
				false);
		ButterKnife.bind(this, view);
		this.inflater = inflater;
		this.container = container;
		init();
		return view;
	}
	
	@Override
	public void onDestroyView() {
		DialogTool.hideDialog(dialog);
		super.onDestroyView();
	}

	private void init() {
		setTop();
//		if (UserInfoUtil.getCurrentUserRole() == AppConfig.PROJECT_MNG) {
//			rlHire.setVisibility(View.VISIBLE);
//		}
//		else {
//			rlHire.setVisibility(View.GONE);
//		}
	}


	/**
	 * @author: HYM
	 * @Title: setTop
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-8-31
	 */
	private void setTop() {
		tvBack.setVisibility(View.GONE);
		tvTitle.setText("业主管理系统");
		Drawable searchDraw = getResources()
				.getDrawable(R.mipmap.icon_tool_escape);
		searchDraw.setBounds(0, 0, searchDraw.getMinimumWidth(),
				searchDraw.getMinimumHeight());
		tvSearch.setCompoundDrawables(searchDraw, null, null, null);
		tvSearch.setText("");
	}

	@OnClick({R.id.tvMaintenance, R.id.tvRepair, R.id.tvHireRelate, R.id.tvUserCheck,R.id.tvSearch,R.id.tvDataAnalsis,R.id.tvCheck })
	public void onClick(View view) {
		Intent intent = null;
		switch (view.getId()) {
		case R.id.tvDataAnalsis:
			// 数据分析
			intent = new Intent(getContext(), OwnerDataAnalysisActivity.class);
			startActivity(intent);
			break;
		case R.id.tvUserCheck:
			// 用户信息审核
			intent = new Intent(getContext(),UserInfoCheckActivity.class);
			startActivity(intent);
			break;
		case R.id.tvMaintenance:
			// 维保任务
			intent = new Intent(getContext(),
					OwnerMaintenanceTaskActivity.class);
			startActivity(intent);
			break;
		case R.id.tvRepair:
			// 维修任务
			intent = new Intent(getContext(), OwnerMaintenanceRepairListActivity.class);
			startActivity(intent);
			break;
		case R.id.tvSearch:
			// 逃生定位
			intent = new Intent(getContext(), OwnerListWithMapActivity.class);
			startActivity(intent);
			break;
		case R.id.tvHireRelate:
			//雇佣关系
			getHireInfo();
			break;
		case R.id.tvCheck:
			//检测报告
			intent = new Intent(getContext(), OwnerCheckReportActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
	
	public void getHireInfo()
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		
		dialog = DialogTool.showDialog(dialog, getContext());
		VolleyRequest.StringRequestPost(getContext(), WebApi.OWNER_HIRE_INFO, "", params,
				new StringInterface(getContext()) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								String data = obj.getString("data");
								HireUnitBean temp = new Gson().fromJson(data, HireUnitBean.class);
								if (temp == null) {
									Intent intent = new Intent(getContext(), AddHireActivity.class);
									startActivity(intent);
								}
								else {
									Intent intent = new Intent(getContext(), HireDetailActivity.class);
									intent.putExtra("hire_unit", data);
									startActivity(intent);
								}
							}
							else {
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						finally
						{
							DialogTool.hideDialog(dialog);
						}
						
					}

					@Override
					public void stringError(VolleyError error) {
						DialogTool.hideDialog(dialog);
					}
				});
	}
}
